package com.insurance.service.impl;

import com.insurance.mapper.CouponMapper;
import com.insurance.pojo.CouponsInfo;
import com.insurance.pojo.User;
import com.insurance.pojo.User1;
import com.insurance.pojo.User_coupon;
import com.insurance.service.CouponService;
import com.insurance.utils.ConversionDateUtils;
import org.junit.Test;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User1: 李辽
 * Date: 2020/10/27 17:07
 * Description:
 * Version: V1.0
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired(required = false)
    private CouponMapper couponMapper;


    @Override
    public List<Map> queryCoupon(String start_time, String end_time, Integer disbursement_id, Integer usearea_id) {
        Date new_start_time = null;
        Date new_end_time = null;
            if (start_time != null ){
                //将从前端获取到的String类型转换为Date类型。便于比较。
                new_start_time  = ConversionDateUtils.conversionDateUtils(start_time);
            }
           if (end_time != null){
               new_end_time  = ConversionDateUtils.conversionDateUtils(end_time);
           }

        List<Map> mapList = couponMapper.queryCoupon(new_start_time,new_end_time,disbursement_id,usearea_id);
        for (Map map:mapList) {
            System.out.println("=======service中查询出来的数据为："+map);
        }
        for (Map map:mapList) {
            String start_date = ConversionDateUtils.conversionDateStringUtils(map.get("start_time"));
            map.put("start_time",start_date);
        }
        for (Map map:mapList) {
            String end_date = ConversionDateUtils.conversionDateStringUtils(map.get("end_time"));
            map.put("end_time",end_date);
        }
        return mapList;
    }

    @Override
    public List<Map> qeuryDisbursement() {
        return couponMapper.qeuryDisbursement();
    }

    @Override
    public List<Map> qeuryGetway() {
        return couponMapper.qeuryGetway();
    }

    @Override
    public List<Map> qeuryUseArea() {
        return couponMapper.qeuryUseArea();
    }

    @Override
    public int editCoupon(CouponsInfo couponsInfo) {

        System.out.println("进入更改后台service层。数据为："+couponsInfo);
        return couponMapper.editCoupon(couponsInfo);
    }



    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int sendCoupon(User_coupon user_coupon) {
        User1 user1 = couponMapper.queryUserId(user_coupon.getUser_phone());
        int user_id = user1.getUser_id();
        System.out.println("通过用户电话号码获取当前用户id" +user_id);

        user_coupon.setUser_id(user_id);

        int result = couponMapper.sendCoupon(user_coupon);

        //获取新插入的user_coupon_id
        int user_coupon_id = user_coupon.getUser_coupon_id();

        System.out.println("获取新插入的user_coupon_id："+user_coupon_id);
        int result1 = 0;
        List<Map> list = new ArrayList<Map>();
        if (result > 0){
            //获取当前用户的优惠卷数量
            list = couponMapper.queryTotalCoupon(user_coupon);
            if (list.size() != 0){//防止空指针问题
                System.out.println("获取当前用户的优惠卷数量:"+list.size());
                int totalCounpon= list.size();
                //更新当前用户获取的优惠卷
                result1 = couponMapper.updateTotalNumbler(user_coupon_id,totalCounpon);
                System.out.println("更新当前用户获取的优惠卷:"+result1);
            }else {
                result1 = 0;
            }

        }

        return result1;
    }

    @Override
    public int judgePhone(String user_phone) {
        List<Map> mapList = couponMapper.judgePhone(user_phone);
        int result = mapList.size();
        return result;
    }

    @Override
    public User findUserId(User_coupon user_coupon) {
        return null;
    }


    @Override
    public int addCoupon(CouponsInfo couponsInfo) {
        System.out.println("进入serviceimpl层，数据为："+couponsInfo);
        System.out.println("couponsInfo.getDisbursementId()："+couponsInfo.getDisbursementId());
        int result = 0;
        if (couponsInfo.getDisbursementId() == 0){//手动发送优惠卷
            //添加优惠卷信息
            result = couponMapper.addCoupon(couponsInfo);
        }else{//全局发送，每个用户增加一张优惠卷
            System.out.println("进入全局发送模式："+couponsInfo.getDisbursementId());
            //添加优惠卷信息
            result = couponMapper.addCoupon(couponsInfo);
            //获取新插入的优惠卷信息id
            int coupon_id = couponsInfo.getCoupon_id();
            System.out.println("=====获取新插入的优惠卷信息id====="+coupon_id);
            //查询所有用户id
            List<Map> mapList = couponMapper.qeuryAllUserId();
            for (Map map:mapList) {
                int user_id = (int) map.get("user_id");
                //将刚刚插入的优惠卷发送给所有普通用户
                result = couponMapper.sendAllCouponToUser(coupon_id,user_id);
            }
        }
        return result;
    }

    }








